"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DESC/DESCRIBE
Case Name   : 非B库中describe查询表结构,合理报错
Create At   : 2022-10-09
Owner       : @zou_jialiang050
Description :
    1.建表
    2.describe分析表结构
    3.清理环境
Expect      :
    1.成功
    2.合理报错
    3.成功
History     : 
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DESCRIBE(unittest.TestCase):
    def setUp(self):
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.table = 't_compatibility_describe_0012'

    def test_describe(self):
        text1 = 'step1:建表 expect:成功'
        self.log.info(text1)
        create_table = self.pri_sh.execut_db_sql(
            f'''drop table if exists {self.table};
            create table {self.table}(c_int int default null,
            c_test varchar(10) default ' ');''')
        self.log.info(create_table)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, create_table,
                      '执行失败' + text1)

        text2 = 'step2:describe分析表结构 expect:合理报错'
        self.log.info(text2)
        describe_table = self.pri_sh.execut_db_sql(
            f'''describe {self.table};''')
        self.log.info(describe_table)
        self.assertIn('ERROR:  syntax error at or near "describe"',
                      describe_table, '执行失败' + text2)

    def tearDown(self):
        text3 = 'step3:清理环境 expect:成功'
        self.log.info(text3)
        clean_environment = self.pri_sh.execut_db_sql(
            f'''drop table {self.table};''')
        self.log.info(clean_environment)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, clean_environment,
                      '执行失败' + text3)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

